﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace MotionControl.Modules.PLC.Business
{
    [JsonObject(MemberSerialization.OptIn)]
    public class BoolType : INotifyPropertyChanged
    {
        private string _name;
 
        [JsonProperty]
        public string Name { get; set; }

        private bool _boolValue;
        [JsonProperty]
        public bool BoolValue
        {
            get { return _boolValue; }
            set
            {
                if (_boolValue == value)
                    return;
                _boolValue = value;
                boolTypeAction?.Invoke(Name, BoolValue);
                boolSaveAction?.Invoke();
                OnPropertyChanged("BoolValue");
            }
        }

        public Action<string, bool> boolTypeAction;
        public Action boolSaveAction;

        public BoolType(string name, bool enable = false)
        {
            Name = name;
            BoolValue = enable;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string info)
        {
            var handler = PropertyChanged;
            handler?.Invoke(this, new PropertyChangedEventArgs(info));
        }
    }

    [JsonObject(MemberSerialization.OptIn)]
    public class FloatType : INotifyPropertyChanged
    {

        [JsonProperty]
        public string Name { get; set; }

        public string Suffix { get; set; }

        private float _floatValue;
        [JsonProperty]
        public float FloatValue
        {
            get { return _floatValue; }
            set
            {
                if (_floatValue == value)
                    return;
                _floatValue = value;
                floatTypeAction?.Invoke(Name, FloatValue);
                saveAction?.Invoke();
                OnPropertyChanged("FloatValue");
            }
        }

        public Action<string, float> floatTypeAction;
        public Action saveAction;

        public FloatType(string name, string suffix = "", float val = 0)
        {
            Name = name;
            FloatValue = val;
            Suffix = suffix;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string info)
        {
            var handler = PropertyChanged;
            handler?.Invoke(this, new PropertyChangedEventArgs(info));
        }
    }
}
